<?php
//require_once __DIR__ . '/HuishengMysql.php';
require_once __DIR__ . '/HuishengRedis.php';

class CaptureFileHandler
{
    private $db;
    private $extension;
    private $finalformatfile;
    private $finalfile;
    private $speech_id;
    public function __construct(HuishengMysql $db)
    {
        $this->db = $db;
        $this->extension = "wav";
    }

    //遍历文件夹
    public static function listAllFiles($from)
    {
        $files = array();
        if (!is_dir($from)) {
            return $files;
        }
        $dirs = array($from);
        while (NULL !== ($dir = array_pop($dirs))) {
            if ($dh = opendir($dir)) {
                while (false !== ($file = readdir($dh))) {
                    if ($file == '.' || $file == '..')
                        continue;
                    $path = $dir . '/' . $file;
                    if (is_dir($path))
                        $dirs[] = $path;
                    else
                        $files[] = $path;
                }
                closedir($dh);
            }
        }
        return $files;
    }

    public function getFinalFormatFile()
    {
        return $this->finalformatfile;
    }

    //开始处理采集音频
    public function CaptureFileHandle($file)
    {
        $this->filenameFormat(file_get_contents($file));
        if (is_file($this->finalfile)) {
            $this->createDataAndMoveFile();
            $this->pushOffTransQue();
        }
    }

    //格式化文件路径
    private function filenameFormat($filename)
    {
        $config = require __DIR__ . '/config.php';
        $this->finalfile = $config['share_path'] . substr($filename, strpos($filename, '/ftp'));
        $this->finalformatfile = trim($config['share_path'] . $config['box_recording'] . date('YmdHis', time()) . rand(1000, 9999) . '.' . $this->extension);
    }

    private function createDataAndMoveFile()
    {
        if ($this->createFileData()) {
            $this->moveFile();
        }
    }


    private function pushOffTransQue()
    {
        $arr = [
            'id' => $this->speech_id,
            'file' => pathinfo($this->finalformatfile)['basename'],
            'type' => 2 //盒子实时上传文件
        ];
        $config = require __DIR__ . '/config.php';
        $redis = new HuishengRedis($config['redis']);
        $data = $redis->offTransPush($arr);
        echo "文件".$arr['file']."已加入转写队列。。\n";
    }

    private function createFileData()
    {
        $mid = explode('/', $this->finalfile)[6];
        $liveSpeech_data = $this->db->getLiveSpeechDataByMid($mid);
        if ($liveSpeech_data) {
            $this->speech_id = $this->insertData($liveSpeech_data);
            return true;
        }
        return false;
    }

    private function insertData($speech_data)
    {
        $params = [
            'meeting_id' => $speech_data['id'],
            'user_id' => $speech_data['uid'],
            'device_id' => $speech_data['did'],
            'lvsr_start_time' => $speech_data['start_time'],
            'formatname' => pathinfo($this->finalformatfile)['basename'],
            'filename' => pathinfo($this->finalfile)['basename'],
            'create_time' => time(),
            'update_time' => time(),
        ];
        $insert_id = $this->db->insertDetailToSpeech($params);
        return $insert_id;
    }

    // protected function moveFile()
    // {   
    //     $operat = new AudioFileAllOperation();
    //     $from = $this->finalfile;
    //     $to = $this->finalformatfile;
    //     $operat->moveFileandTrans2Wav($from, $to);
    // } 

    private function moveFile()
    {
        $from = $this->finalfile;
        $to = $this->finalformatfile;
        exec("cp $from $to");
    }
}
